<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var
include_once("Smarty.class.php");				//--> out template
include_once("Common.php");
include_once("UploadFiles.php");				//--> UploadFiles
include_once("customerSession.inc.php");
include_once ("Session.php");					//-- Session
include_once("Country.php");					//--> Country
include_once("AddressBook.php");				//--> AddressBook
include_once("Zone.php");						//--> Zone

include_once("Strings.php");					//--> String utils

include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart

require_once('includeHttps.php');

$common 		= new Common();
$objSession		= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session
$objCountry		= new Country(0);									//--> Country
$objAddressBook	= new AddressBook(0);								//--> AddressBook
$objZone		= new Zone(0);										//--> Zone

$objCart 		= new Cart();		//--> Cart
$objStrings 	= new Strings();

$customerid 	= 0;

/*----- check session -----*/
if($objSession->exist()) {
	$customerid = $objSession->getUserID();
}else{
	$location = './myaccount.php';
	
	header("Location: $location");
	exit;
}

if($_SESSION['cart'])
	$objCart = unserialize($_SESSION['cart']);

$firstname 		= '';
$lastname 		= '';
$company		= '';
$postcode		= '';

$state			= '';
$city			= '';
$street			= '';

$zoneid			= 0;

if(isset($_POST['countryId']))
	$countryId = $_POST['countryId'];
else
	$countryId	= 223;
	
$firstname 		= $_POST['firstname'];
$lastname 		= $_POST['lastname'];
$countryId		= $_POST['countryId'];
$company		= '';
$postcode		= $_POST['postcode'];


$city			= $_POST['city'];
$street			= $_POST['street'];
		

$state			= $_POST['state'];
$zoneid			= $_POST['zoneid'];


/*----- add new address book -----*/
if(isset($_POST['action']) && $_POST['action'] == 'Submit'){
	$gender			= '';
	
	if(empty($zoneid))
		$zoneid	= $ZONE_ID;

	if(empty($firstname))
		$error_message = 'The first name should\'t be empty.';
	else if(empty($lastname)){
		$error_message = 'The last name should\'t be empty.';
	}else if(empty($street)){
		$error_message = 'The street address should\'t be empty.';
	}else if(empty($postcode)){
		$error_message = 'The postcode should\'t be empty.';
	}else if(empty($city)){
		$error_message = 'The city should\'t be empty.';
	}
	
	if(empty($state) && !empty($zoneid)){
		$sql = "SELECT name FROM zones WHERE id = $zoneid";
		$state = $common->getColumn(DB_TAG_PUBLIC, $sql);
	}
	
	
	if(empty($error_message)){
		$id = $objAddressBook->add($customerid, $gender, $company, $firstname, $lastname, $street, $suburb, $postcode, $city, $state, $countryId, $zoneid);
		
		if($id){
			if(!empty($primary)){
				$sql = "UPDATE customers SET modifiedTime = UTC_TIMESTAMP(), default_address_id = '$id' WHERE id = $customerid";
				$isSuccess = $common->update(DB_TAG_SYSTEM, $sql);
			}
			
			$location = './addressBookList.php';
	
			header("Location: $location");
			exit;
		}else{
			$error_message = 'Add failure.';
		}
	}
}


include_once("includeCategory.php");	//--> include category
include_once("includeSpec.php");		//--> include spec

$arrCountries = $objCountry->getGroupList();

/*-- get zone -----*/
	
$arrZones = $objZone->getGroupList($countryId);
if(count($arrZones) > 0)
	$hasZone = 'yes';


/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

$smarty->force_compile 	= true;
$smarty->debugging 		= false;
$smarty->caching 		= false;
$smarty->cache_lifetime = 120;

$smarty->assign('error_message', $error_message);

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL);

$smarty->assign('categorys', $categorys);
$smarty->assign('topCategory', $topCategory);
$smarty->assign('specProducts', $specProducts);

$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);


$smarty->assign('countryId', $countryId);
$smarty->assign('arrCountries', $arrCountries);

$smarty->assign('firstname', $firstname);
$smarty->assign('lastname', $lastname);

$smarty->assign('telephone', $telephone);
$smarty->assign('fax', $fax);
$smarty->assign('company', $company);
$smarty->assign('postcode', $postcode);
$smarty->assign('state', $state);
$smarty->assign('city', $city);
$smarty->assign('street', $street);


$smarty->assign('hasZone', $hasZone);
$smarty->assign('arrZones', $arrZones);
$smarty->assign('zoneid', $zoneid);


$smarty->display('addressBookAdd.html');
?>
